//
//  HOExtraAssCheckViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 7/4/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOExtraAssCheckViewController.h"
#import "HOConstants.h"
#import "HOEditView.h"
#import "HOUtils.h"
#import "HOExtraAssistantEntity.h"
#import "HOStatusEntity.h"
#import "HOStatusTableViewCell.h"
#import "HOButton.h"
#import "UIImageView+WebCache.h"
#import "HOSearchContactViewController.h"
#import "HOContactEntity.h"
#import "HOImageMgr.h"

@interface HOExtraAssCheckViewController ()<HOEditViewDelegate, UITextViewDelegate, UITextFieldDelegate, HOSearchContactViewControllerDelegate>
@property(nonatomic, retain)HOEditView *replyEditView;
@property(nonatomic, retain)HOButton *revokeBtn;
@property(nonatomic, retain)HOButton *checkPassBtn;
@property(nonatomic, retain)UILabel *userNameTxt;
@property(nonatomic, retain)UILabel *telephoneTxt;
@property(nonatomic, retain)UIImageView *headerImgView;
@property(nonatomic, retain)UIView *btnView;
@property(nonatomic, retain)UIView *contactView;
@property(nonatomic, retain)HOContactEntity *contactEntity;
@end

@implementation HOExtraAssCheckViewController


- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	self.contactEntity = [[[HOContactEntity alloc] init] autorelease];
	[self initSubviews];
}

- (void)initSubviews
{
	self.replyEditView = [[[HOEditView alloc] initWithFrame:CGRectMake(0,
																	   0,
																	   self.view.frame.size.width,
																	   134)] autorelease];
	self.replyEditView.limitedCount = 120;
	self.replyEditView.delegate = self;
	self.replyEditView.layer.cornerRadius = 0;
	self.replyEditView.layer.borderWidth = 1;
	[self.replyEditView.layer setMasksToBounds:YES];
	[self.replyEditView setPlaceHolder:NSLocalizedString(@"请输入审核信息", @"请输入审核信息")];
	[self.replyEditView getTextView].returnKeyType = UIReturnKeySend;
	[self.view addSubview:self.replyEditView];
	
	self.contactView = [[[UIView alloc]initWithFrame:CGRectMake(-1,
															self.replyEditView.frame.size.height + self.replyEditView.frame.origin.y + HO_HORIZONAL_PADDING,
															self.view.frame.size.width + 2,
															56)] autorelease];
	
	
	self.contactView.layer.borderWidth = [HOUtils defaultOnePixelConversion];
	[self.contactView setBackgroundColor:[UIColor whiteColor]];
	self.contactView.layer.borderColor = TEXT_COLOR_E3E3E3.CGColor;
	
//	UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(self.contactView.frame.size.width - 12,
//															 (56 - 16) / 2,
//															 9,
//															 16)] autorelease];
//	imgView.image = [UIImage imageNamed:@"ic_taizhuanyemian"];
//	[self.contactView addSubview:imgView];
	
	HOButton *btn = [[[HOButton alloc] initWithFrame:CGRectMake(0, 0, self.contactView.frame.size.width, 56)] autorelease];
	[btn setBackgroundColor:[UIColor clearColor]];
	[btn addTarget:self action:@selector(didClickContact:) forControlEvents:UIControlEventTouchUpInside];
	[self.contactView addSubview:btn];
	
	self.headerImgView = [[[UIImageView alloc] initWithFrame:CGRectMake(HO_HORIZONAL_PADDING, 4, 48, 48)] autorelease];
	self.headerImgView.layer.cornerRadius = 5;
	[self.contactView addSubview:self.headerImgView];
	[self.view addSubview:self.contactView];
	
	
	self.userNameTxt = [[[UILabel alloc] initWithFrame:CGRectMake(HO_HORIZONAL_PADDING, 17, self.view.frame.size.width - 40 - 48, 22)] autorelease];
	[self.userNameTxt setTextColor:TEXT_COLOR_666666];
	[self.userNameTxt setFont:[UIFont systemFontOfSize:15]];
	[self.contactView addSubview:self.userNameTxt];
	self.userNameTxt.text = NSLocalizedString(@"添加分管院长", @"添加分管院长");
	
	self.telephoneTxt = [[[UILabel alloc] initWithFrame:CGRectMake(HO_HORIZONAL_PADDING * 2 + 48, 28, self.view.frame.size.width - 40 - 48, 22)] autorelease];
	[self.telephoneTxt setTextColor:TEXT_COLOR_999999];
	[self.telephoneTxt setFont:[UIFont systemFontOfSize:15]];
	[self.contactView addSubview:self.telephoneTxt];
	
//	self.telephoneTxt.layer.borderWidth = 1;
//	self.userNameTxt.layer.borderWidth = 1;
//	self.headerImgView.layer.borderWidth = 1;
	
	
	self.btnView = [[[UIView alloc]initWithFrame:CGRectMake(0,
													   self.contactView.frame.size.height + self.contactView.frame.origin.y + HO_HORIZONAL_PADDING,
															self.view.frame.size.width,
															44)] autorelease];
	self.revokeBtn = [HOButton buttonWithType:UIButtonTypeCustom];
	self.revokeBtn.frame = CGRectMake(HO_HORIZONAL_PADDING,
								 2,
								 (self.view.frame.size.width - 3 * HO_HORIZONAL_PADDING) / 2,
								 40);
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
	[self.revokeBtn setTintColor:[UIColor whiteColor]];
	[self.revokeBtn setTitle:@"打回" forState:UIControlStateNormal];
	[self.revokeBtn addTarget:self action:@selector(revokeEntity:) forControlEvents:UIControlEventTouchUpInside];
	[self.revokeBtn setBackgroundImage:[HOUtils createImageWithColor:UIColorFromRGB(0xec6084) forView:self.revokeBtn] forState:UIControlStateNormal];
	[self.revokeBtn setBackgroundImage:[HOUtils createImageWithColor:TEXT_GRAY_DARK_dbdbdb forView:self.revokeBtn] forState:UIControlStateDisabled];
	self.revokeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
	[self.revokeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	self.revokeBtn.layer.cornerRadius = 3;
	self.revokeBtn.tag = 111;
	[self.revokeBtn.layer setMasksToBounds:YES];
	[self.btnView addSubview:self.revokeBtn];
	
	self.checkPassBtn = [HOButton buttonWithType:UIButtonTypeCustom];
	self.checkPassBtn.frame = CGRectMake(self.revokeBtn.frame.size.width + 2 * HO_HORIZONAL_PADDING,
									self.revokeBtn.frame.origin.y,
								 self.revokeBtn.frame.size.width,
								 40);
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
	[self.checkPassBtn setTintColor:[UIColor whiteColor]];
	[self.checkPassBtn setTitle:@"审核通过" forState:UIControlStateNormal];
	[self.checkPassBtn addTarget:self action:@selector(checkPassEntity:) forControlEvents:UIControlEventTouchUpInside];
	[self.checkPassBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:self.checkPassBtn] forState:UIControlStateNormal];
	[self.checkPassBtn setBackgroundImage:[HOUtils createImageWithColor:TEXT_GRAY_DARK_dbdbdb forView:self.checkPassBtn] forState:UIControlStateDisabled];
	self.checkPassBtn.titleLabel.font = [UIFont systemFontOfSize:15];
	[self.checkPassBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	self.checkPassBtn.layer.cornerRadius = 3;
	[self.checkPassBtn.layer setMasksToBounds:YES];
	[self.btnView addSubview:self.checkPassBtn];
	
	[self.view addSubview:self.btnView];
}

- (void)dealloc
{
	self.btnView = nil;
	self.checkPassBtn = nil;
	self.revokeBtn = nil;
	self.contactView = nil;
	self.userNameTxt = nil;
	self.telephoneTxt = nil;
	self.contactEntity = nil;self.extraAssistantEntity = nil;
	self.replyEditView = nil;
	[super dealloc];
}

- (void)didClickContact:(id)sender
{
	HOSearchContactViewController *viewCtrl = [[[HOSearchContactViewController alloc] init] autorelease];
	viewCtrl.delegate = self;
	[self.navigationController pushViewController:viewCtrl animated:YES];
}

- (void)didSelectContact:(HOContactEntity *)contactEntity
{
	self.contactEntity = contactEntity;
	self.userNameTxt.frame = CGRectMake(HO_HORIZONAL_PADDING * 2 + 48, 4, self.view.frame.size.width - 40 - 48, 22);
	self.userNameTxt.text = [NSString stringWithFormat:@"%@ - %@", self.contactEntity.userName, self.contactEntity.departmentName];
	self.telephoneTxt.text = self.contactEntity.telephone;
	[self.headerImgView sd_setImageWithURL:[NSURL URLWithString:self.contactEntity.headPic] placeholderImage:[[HOImageMgr sharedInstance] getDefaultImage]];
	
}

- (void)revokeEntity:(id)sender
{
	//审核未通过
	if(self.isDirUser)
	{
		//主任审核未通过
		[self requestDataWithAction:kHO_ACTION_ExternalOrderDirectorApproval withStatus:HO_EXTRA_ASS_ORDER_CHECK_FAILED];
	}
	else
	{
		//院长审核未通过
		[self requestDataWithAction:kHO_ACTION_ExternalOrderDeanApproval withStatus:HO_EXTRA_ASS_ORDER_CHECK_FAILED];
	}
}

- (void)checkPassEntity:(id)sender
{
	//审核通过
	if(self.isDirUser)
	{
		//主任审核通过
		[self requestDataWithAction:kHO_ACTION_ExternalOrderDirectorApproval withStatus:HO_EXTRA_ASS_ORDER_DIR_CHECK];
	}
	else
	{
		//院长审核通过
		[self requestDataWithAction:kHO_ACTION_ExternalOrderDeanApproval withStatus:HO_EXTRA_ASS_ORDER_DEAN_CHECK];
	}
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
	return YES;
}

- (void)hideKeyboard
{
	[[self.replyEditView getTextView] resignFirstResponder];
}

- (void)requestDataWithAction:(NSString *)action withStatus:(int)status
{
	NSString *content = [HOUtils getValideString:[self.replyEditView getText]];
	if(content.length == 0)
		content = @"无";
	NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{kHO_FIELD_Action:action,
																			   kHO_FIELD_OrderId:[NSNumber numberWithInt:self.extraAssistantEntity.orderId],
																			   kHO_FIELD_Content:content,
																			   
																			   kHO_FIELD_Status: [NSNumber numberWithInt:status]}];
	if([action isEqualToString:kHO_ACTION_ExternalOrderDirectorApproval] && status == HO_EXTRA_ASS_ORDER_DIR_CHECK)
	{
		if(self.contactEntity.userId != 0)
		{
			[dic setObject:[NSNumber numberWithInt:self.contactEntity.userId] forKey:kHO_FIELD_DeanId];
		}
	}
	[self sendRequestWithParams:dic];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"审核外协单", @"审核外协单")];
	
	if(self.isDirUser == NO)
	{
		self.btnView.frame = CGRectMake(0,
										self.replyEditView.frame.size.height + self.replyEditView.frame.origin.y + HO_HORIZONAL_PADDING,
										self.view.frame.size.width,
										44);
	}
}

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_ExternalOrderDeanApproval]
	   || [response isEqualAction:kHO_ACTION_ExternalOrderDirectorApproval])
	{
		self.view.userInteractionEnabled = NO;
		[self sendCommand:kHO_CMD_Report_Detail_Changed data:nil];
		[self popupHintView:NSLocalizedString(@"提交成功!", @"提交成功!") inTime:1.5 needCallback:YES];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}

@end

